/**
 * Torchd UI Dialog Window
 *
 * Opens a scalable dialog window
 *
 * @package Torchd Framework
 * @subpackage UI
 * @author Jason Raede <jason@torchdm.com>
 */

torchd.ui.dialog = function(params) {
	this.init(params);
};

/** STATIC METHODS */
torchd.ui.dialog.sizes = {
	small:['400', '200'],
	medium:['500', '250'],
	large:['700', '350'],
	full:['900', '450']
};

torchd.ui.dialog.parseSize = function(size) {
	if(['small', 'medium', 'large', 'full'].indexOf(size)) {
		return this.sizes[size];
	}
	else if(typeof size === 'array' && size.length == 2) {
    	return size;
    }
    else {
    	return false;
    }
};

torchd.ui.dialog.defaultParams = {
	title:'',
	mode:'default',
	modal:true,
	size:'medium',
	type:'alert'
};

/** INSTANCE METHODS */

/**
 * Initialize the dialog with the parameters
 *
 * @param object params
 */
torchd.ui.dialog.prototype.init = function(params) {
	var self = this;
	this.title = params.title;
	
	var params = $.extend({}, torchd.ui.dialog.defaultParams, params);
	
	// Create the element
	this.window = torchd.ui.createElement('div').addClass('torchd-ui-dialog');
	
	
	
	
	
	// Title
	torchd.ui.createElement('h3').addClass('torchd-ui-widget-header').html(params.title).appendTo(this.window);
	
	// Inner content
	this.content = $('<div/>').addClass('torchd-ui-inner').appendTo(this.window);
	if(params.content) this.content.html(params.content);
	
	if(params.buttons) this.buttons = $('<div/>').addClass('torchd-ui-dialog-buttons').appendTo(this.window);
	// Message?
	if(params.message) {
		if(typeof params.message == 'object') {
			$('<p/>').addClass('torchd-ui-message-' + params.message.type).html(params.message.text).appendTo(this.content);
		}
		else if(typeof params.message == 'string') {
			$('<p/>').addClass('torchd-ui-message-normal').html(params.message).appendTo(this.content);
		}
		else {
			torchd.system.error('ui.dialog.init', 'Message type must be an object or a string.');
		}
		
	}
	this.window.hide().appendTo('body');
	// Type
	switch(params.type) {
		case 'ajax':
			$.ajax({
				url:params.ajaxUrl,
				type:'GET',
				dataType:'json',
				beforeSend:function() {
					self.content.html('<div class="torchd-ui-ajax-loader"></div>');
					self.window.css({
						marginTop:'-'+(parseInt(self.window.realHeight())/2).toString()+'px',
						marginLeft:'-'+(parseInt(self.window.realWidth())/2).toString()+'px',
					});
				},
				success:function(response) {
					self.content.html(response.content);
					if(params.buttons) {
						for(var i=0;i<params.buttons.length;i++) {
							if(!response['hide_'+params.buttons[i].id]) {
								var okButton = $('<button/>').attr('id', params.buttons[i].id).data('dialog', self).html(params.buttons[i].title).click(params.buttons[i].clickAction).appendTo(self.buttons);
							}
						}
					}
					self.window.css({
						marginTop:'-'+(parseInt(self.window.realHeight())/2).toString()+'px',
						marginLeft:'-'+(parseInt(self.window.realWidth())/2).toString()+'px',
					});
					
				}
			});
			
			
			break;
		case 'alert':
		default:
			var okButton = $('<button/>').data('dialog', this).html('OK').click(function() {
				$(this).data('dialog').close();
			}).appendTo(this.content);
			break;
		case 'ajax-loader':
			this.content.html('<div class="torchd-ui-ajax-loader"></div>');
			break;
		case 'confirm':
			var cancelButton = $('<button/>').data('dialog', this).html('Cancel').click(function() {
				if(params.onCancel) params.onCancel();
				$(this).data('dialog').close();
			}).appendTo(this.content);
			var okButton = $('<button/>').data('dialog', this).html('OK').click(function() {
				if(params.onSubmit) params.onSubmit($(this).data('dialog').content);
				$(this).data('dialog').close();
			}).appendTo(this.content);
			break;
		case 'form':
			var form = new torchd.form(params.formElements);
			form.insert(this.content);
			var cancelButton = $('<button/>').data('dialog', this).html('Cancel').click(function() {
				if(params.onCancel) params.onCancel();
				$(this).data('dialog').close();
			}).appendTo(this.content);
			
			var submitButton = $('<button/>').data('dialog', this).html('Submit').click(function() {
				if(params.onSubmit) {
					params.onSubmit(form.getData());
				}
				$(this).data('dialog').close();				
				
			}).appendTo(this.content);
			break;
		case 'custom':
			params.construct(this);
			break;
		case 'prompt':
			// One field, two buttons (cancel + submit)
			if(params.defaultValue) {
				var field = new torchd.form.field({
					name:'',
					type:'text',
					data:'varchar',
					fields:[{
						name:'prompt',
						defaultValue:params.defaultValue
					}]
				});
			}
			else {
				var field = new torchd.form.field({
					name:'',
					type:'text',
					data:'varchar',
					fields:[{
						name:'prompt'
					}]
				});
			}
			field.appendTo(this.content);
			var cancelButton = $('<button/>').data('dialog', this).html('Cancel').click(function() {
				if(params.onCancel) params.onCancel();
				$(this).data('dialog').close();
			}).appendTo(this.content);
			
			var submitButton = $('<button/>').data('dialog', this).html('Submit').click(function() {
				if(params.onSubmit) {
					params.onSubmit(field.getData());
				}
				$(this).data('dialog').close();				
				
			}).appendTo(this.content);
			break;
	}
	
	// Size
	
	var size = torchd.ui.dialog.parseSize(params.size);
	if(size) {
		this.window.css({
			width:size[0],
			height:size[1],
			marginTop:'-'+(parseInt(size[1])/2).toString()+'px',
			marginLeft:'-'+(parseInt(size[0])/2).toString()+'px'
		});
	}
	else if(params.type != 'ajax'){
		this.window.css({
			marginTop:'-'+(parseInt(this.window.realHeight())/2).toString()+'px',
			marginLeft:'-'+(parseInt(this.window.realWidth())/2).toString()+'px',
		});
	}
	
	this.window.fadeIn(200);
};
/**
 * Display a loading animation in the inner dialog 
 */
torchd.ui.dialog.prototype.ajaxLoader = function() {
	this.content.html('<div class="torchd-ui-ajax-loader"></div>');
	this.window.css({
		marginTop:'-'+(parseInt(this.window.realHeight())/2).toString()+'px',
		marginLeft:'-'+(parseInt(this.window.realWidth())/2).toString()+'px',
	});
};

/**
 * Close the dialog window and release it from memory
 */
torchd.ui.dialog.prototype.close = function() {
	var self = this;
	this.window.fadeOut(100, function() {
		$(this).remove();
		delete self.window;
		delete self.content;
	});
	this.mask.remove();
	torchd.ui.dialogClosed(this.title);
};


